<?php

namespace app\enterprise\controller;

use app\adminapi\controller\AuthController;
use think\exception\ValidateException;
use app\enterprise\model\department\Department as departmentModel;
use app\enterprise\model\User as userModel;
use app\enterprise\model\Parameter as parameterModel;

/**
 * 账号管理
 */
class User extends AuthController
{
    public function page()
    {
        $param = $this->request->param();
        $where[] = ['status','=',1];
        if (!empty($param['company_name'])) {
            $where[] = ['company_name	', 'like', '%' . $param['company_name'] . '%'];
        }
//        if (!empty($param['xkz'])) {
//            $where[] = ['xkz', 'like', '%' . $param['xkz'] . '%'];
//        }


        $where[] = ['creator_id', '=', $this->adminId];

        $size = $param['size'] ?? '';

        $data = [];

        if (!empty($size)) {
            $list = userModel::getPage($where, $param['size']);
            $data = $list->items();
            foreach ($data as $k=>$var) {
                $info = departmentModel::where('id', $var['pid'])->find();
                if($info){
                    $data[$k]['user_enterprise'] = $info['department_name'];
                }
                if($info['pid']){
                    $pinfo = departmentModel::where('id', $info['pid'])->find();
                    if($pinfo){
                        $data[$k]['user_enterprise'] = $pinfo['department_name'].'/'.$data[$k]['user_enterprise'] ;
                    }
                }
            }
        } else {
            $list = userModel::where($where)->select();
            foreach ($list as $k=>$var) {
                $info = departmentModel::where('id', $var['pid'])->find();
                if($info){
                    $list[$k]['user_enterprise'] = $info['department_name'];
                }
                if($info['pid']){
                    $pinfo = departmentModel::where('id', $info['pid'])->find();
                    if($pinfo){
                        $list[$k]['user_enterprise'] = $pinfo['department_name'].'/'. $list[$k]['user_enterprise'];
                    }
                }
            }
            $data = $list;
        }

//        var_dump($data);exit;

//        if (!empty($list)) {
//            $paramWhere[] = ['type', 'in', '1,2'];
//            $paramMap = parameterModel::selectParameterMap($paramWhere);
//
//            foreach ($data as $item) {
//                if (!empty($item->type_id)) {
//                    $item->type_name = $paramMap[$item->type_id];
//                } else {
//                    $item->type_name = '未知';
//                }
//
//                if (!empty($item->project_progress_status)) {
//                    $item->project_progress_status_name = $paramMap[$item->project_progress_status];
//                } else {
//                    $item->project_progress_status = '未知';
//                }
//            }
//
//        }


        return app('json')->success([
            'list' => $data,
            'total' => empty($size) ? 0 : $list->total(),
            'total_page'=>empty($size) ? 0 : $list->lastPage(),
        ]);

    }


    public function save()
    {

        $param = $this->request->param();
        $param['company_id'] = $this->adminId;

        $userModel = new userModel();
        $data = $userModel->where('company_name', $param['company_name'])->find();
        if($data && empty($param['id'])){
            return app('json')->fail('账户已存在');
        }
        if(empty($param['id'])){
            if($param['password'] <> $param['password1'] ){
                return app('json')->fail('两次密码不一致');
            }
            $param['password'] = md5($param['password']);
        }else{
            if (!empty($param['tem_field']) && $param['tem_field'] == 1) {


            } else {
                // 只修改密码
                if($param['password2'] <> $param['password3'] ){
                    return app('json')->fail('两次密码不一致');
                }
                $param['password'] = md5($param['password2']);
            }

        }


        try {
            if(empty($param['id'])){
                validate([
                    'company_name|账户' => 'require|max:100',
                    'password|密码' => 'require|min:6',
                    'password1|确认密码' => 'require|min:6',
                    'pids|部门' => 'require',
                ])->check($param);
                $data =array(
                    'company_name'=>$param['company_name'],
                    'password'=>$param['password'],
                    'pid'=>end($param['pids']),
                    'create_time'=>time(),
                );
            }else{
                if (!empty($param['tem_field']) && $param['tem_field'] == 1) {
                    // 是修改部门
                    $data = array(
                        'pid' => $param['pid'],
                        'id'  => $param['id'],
                    );
                } else {
                    // 是修改密码
                    validate([
                        'password2|新密码' => 'require|min:6',
                        'password3|确认新密码' => 'require|min:6',
                    ])->check($param);
                    $data =array(
                        'password'=>$param['password'],
                        'id'=>$param['id'],
                    );
                }
            }

//            $data =array(
//                'company_name'=>$param['company_name'],
//                'password'=>$param['password'],
//                'pid'=>end($param['pids']),
//                'create_time'=>time(),
//            );
//            var_dump($data);exit;
            $userModel = new userModel();
            if (empty($param['id'])) {
                // 企业ID
                $data['company_id'] = $this->adminInfo['company_id'];
                $data['creator_id'] = $this->adminId;
                $userModel->add($data);
            } else {
                $userModel->editPwd($data);
            }
            return app('json')->success('操作成功');
        } catch (ValidateException $exception) {
            return app('json')->fail($exception->getMessage());
        }

    }


    public function info()
    {
        $id = $this->request->param('id');
        $projectModel = new projectModel();
        $data = $projectModel->where('id', $id)->where('company_id', $this->adminId)->find();

        if (!empty($data)) {
            $type = parameterModel::getOne($data['type_id']);
            if (!empty($type)) {
                $data['type_name'] = $type['para_name'];
            }
            $progress = parameterModel::getOne($data['project_progress_status']);
            if (!empty($progress)) {
                $data['project_progress_status_name'] = $progress['para_name'];
            }
        }

        return app('json')->success([
            'info' => $data
        ]);
    }

    public function del()
    {
        $id = input('id', '');
        $model = userModel::getOne($id);
        if (!$model) {
            return app('json')->fail('数据不存在');
        }
//        if ($model->company_id != $this->adminId) {
//            return app('json')->fail('无权操作此数据');
//        }


        userModel::dataDelete($id);

        return app('json')->success('删除成功');

    }



}